package com.alibaba.otter.node.etl.load.loader.mq.rabbit.provider;


import com.alibaba.otter.node.etl.load.loader.mq.rabbit.model.ListenerProperties;
import com.alibaba.otter.shared.common.utils.AddressUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.management.ManagementFactory;

public class DefaultConsumerTagProvider implements ConsumerTagProvider {

    private static final String DELIMITER = "@";

    @Override
    public String provide(ListenerProperties props) {
        String pid = ManagementFactory.getRuntimeMXBean().getName().split(DELIMITER)[0];
        String threadName = Thread.currentThread().getName();
        String consumerTag = pid + "-" + threadName + DELIMITER + AddressUtils.getHostIp();
        String namespace = StringUtils.isNotEmpty(props.getNamespace()) ? props.getNamespace() : "default";
        if (props != null) {
            consumerTag = consumerTag + "-" + namespace +
                "-pc" + props.getPrefetchCnt() +
                "-tc" + props.getThreadCnt();
        }
        return consumerTag;
    }
}
